//
//  Download12PhotosViewController.m
//  GolfFriend
//
//  Created by Vescky on 14-3-2.
//  Copyright (c) 2014年 vescky.org. All rights reserved.
//

#import "Download12PhotosViewController.h"
#import "PhotoSearchResultViewController.h"
#import "SVProgressHUD.h"
#import "QRCodeResultViewController.h"

//#define RealDevice

#ifdef RealDevice
#import "ZBarSDK.h"
#endif

@interface Download12PhotosViewController()
#ifdef RealDevice
<ZBarReaderDelegate>
#endif
{
#ifdef RealDevice
    ZBarReaderViewController *qrcodeScanner;
#endif
    
    bool isScanning;
}
@end

@implementation Download12PhotosViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    [self setTitle:@"十二连拍"];
    [super customBackButton];
    
    //定制自己的风格的  UIBarButtonItem
    UIButton* publishButton= [[UIButton alloc] initWithFrame:CGRectMake(0, 7, 30, 30)];//
    [publishButton setImage:[UIImage imageNamed:@"btn_scan.png"] forState:UIControlStateNormal];
    [publishButton addTarget:self action:@selector(scanAction) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* actionItem= [[UIBarButtonItem alloc] initWithCustomView:publishButton];
    [self.navigationItem setRightBarButtonItem:actionItem];
    isScanning = NO;
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#ifdef RealDevice
- (IBAction)btnAction:(id)sender {
    if (tfSearch.text.length < 1) {
        [SVProgressHUD showErrorWithStatus:@"搜索内容不能为空!!"];
        return;
    }
    PhotoSearchResultViewController *pSearchVc = [[PhotoSearchResultViewController alloc] init];
    pSearchVc.keyword = tfSearch.text;
    [self.navigationController pushViewController:pSearchVc animated:YES];
}

- (void)scanAction {
    if (!isScanning) {
        [tfSearch resignFirstResponder];
        if (!qrcodeScanner) {
            qrcodeScanner = [[ZBarReaderViewController alloc] init];
            qrcodeScanner.view.frame = CGRectMake(10, (self.view.frame.size.height - 300) / 2.0, 300, 300);
        }
        viewMask.hidden = NO;
        qrcodeScanner.readerDelegate = self;
        qrcodeScanner.showsZBarControls = NO;
        qrcodeScanner.supportedOrientationsMask = ZBarOrientationMaskAll;
        [self.view addSubview:qrcodeScanner.view];
        isScanning = YES;
    }
    else {
        isScanning = NO;
        [qrcodeScanner.view removeFromSuperview];
        qrcodeScanner = nil;
        viewMask.hidden = YES;
    }
    
//    [self.navigationController pushViewController:reader animated:YES];
}

// ZBarReaderDelegate
- (void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo: (NSDictionary*)info
{
    // do something useful with results
    id <NSFastEnumeration> syms = [info objectForKey: ZBarReaderControllerResults];
    for(ZBarSymbol *sym in syms) {
        NSLog(@"%@",sym.data);
        viewMask.hidden = YES;
        [qrcodeScanner.view removeFromSuperview];
        QRCodeResultViewController *qrResultVc = [[QRCodeResultViewController alloc] init];
        qrResultVc.sourceLink = sym.data;
        [self.navigationController pushViewController:qrResultVc animated:YES];
        break;
    }
}

#endif

@end
